package com.hlk.admin.controller;

import com.hlk.admin.service.FriendLinkService;
import com.hlk.admin.FriendLinkControllerApi;
import com.hlk.pojo.bo.SaveFriendLinkBO;
import com.hlk.pojo.mo.FriendLinkMO;
import com.hlk.result.Result;
import com.hlk.utils.ConvertUtils;
import com.hlk.utils.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-12-29 11:01
 */
@RestController
public class FriendLinkController implements FriendLinkControllerApi {
    @Autowired
    private FriendLinkService friendLinkService;

    @Override
    public Result saveOrUpdateFriendLink(@Valid SaveFriendLinkBO saveFriendLinkBO, BindingResult result) {
        // 校验参数是否符合
        if (result.hasErrors()) {
            Map<String, String> map = ValidateUtils.checkParams(result);
            return Result.errorMap(map);
        }

        FriendLinkMO friendLinkMO = ConvertUtils.sourceToTarget(saveFriendLinkBO, FriendLinkMO.class);
        friendLinkMO.setCreateTime(new Date());
        friendLinkMO.setUpdateTime(new Date());

        friendLinkService.saveOrUpdateFriendLink(friendLinkMO);


        return Result.ok();
    }

    @Override
    public Result getFriendLinkList() {
        return Result.ok(friendLinkService.findAll());
    }

    @Override
    public Result delete(String linkId) {
        friendLinkService.deleteFriendLikn(linkId);
        return Result.ok();
    }

    @Override
    public Result queryPortalAllFriendLinkList() {
        return Result.ok(friendLinkService.queryPortalAllFriendLinkList());
    }
}
